package com.marketingexpert.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.dao.AuthorityDao;
import com.marketingexpert.domain.User;
import com.marketingexpert.domain.permission.ResourceUseRecord;
import com.marketingexpert.domain.permission.Role;
import com.marketingexpert.domain.permission.UserRole;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.Resources;

public class TimeMessageCountsCheckInterceptor extends
		HandlerInterceptorAdapter {
	private static Logger logger = Logger
			.getLogger(TimeMessageCountsCheckInterceptor.class);

	private AuthorityDao authorityDao;

	private UserService userService;

	private Configuration configuration;

	private String interceptPath;
	
	private String countsFullDispatchUrl;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String reqResource = request.getServletPath();
		if (!reqResource.startsWith(interceptPath)) {
			return true;
		}
		try {
		if(request.getSession().getAttribute("time") != null) {
			String parentUserId = (String) request.getSession().getAttribute("parentUserId");
			UserRole ur = authorityDao.getUserRole(parentUserId);
			
			Role role = authorityDao.getRole(ur.getRoleId());
			
			int timeMsgCountsCanUser = configuration.getCanUseCounts("timingMessage",role.getRoleIdentify());
			
			User parentUser = getUserService().queryUserByUId(parentUserId);

			ResourceUseRecord rur = authorityDao.queryAlreadyUserByPId(parentUser.getId(), "timingMessage");
			
			if (rur != null) {
				if (rur.getUserCounts()- timeMsgCountsCanUser >= 0) {
					request.setAttribute("countsFull", Resources.getString(role.getRoleIdentify() + "_timingMessage"));
					request.getSession().getServletContext().getRequestDispatcher(countsFullDispatchUrl).forward(request, response);
					return false;
				}
			}
			return true;
		}
		return true;
		} catch(MarketingExpertException e) {
			logger.error(e.getMessage(), e);
			request.setAttribute("countsFull", Resources.getString("timeMsgcountsCheckError"));
			request.getSession().getServletContext().getRequestDispatcher(countsFullDispatchUrl).forward(request, response);
			return false;
		}
		
	}

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}

	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public String getInterceptPath() {
		return interceptPath;
	}

	public void setInterceptPath(String interceptPath) {
		this.interceptPath = interceptPath;
	}

	public String getCountsFullDispatchUrl() {
		return countsFullDispatchUrl;
	}

	public void setCountsFullDispatchUrl(String countsFullDispatchUrl) {
		this.countsFullDispatchUrl = countsFullDispatchUrl;
	}

	
}
